package com.company.project.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.company.project.common.utils.KeyValueUtils;
import com.company.project.mapper.MidCheckMapper;
import com.company.project.vo.KeyValueVo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

import java.util.Date;
import java.util.List;
import com.company.project.common.utils.DataResult;

import com.company.project.entity.MidCheckEntity;
import com.company.project.service.MidCheckService;



/**
 * 中期检查
 *
 * @author HKX
 * @email *****@mail.com
 * @date 2022-04-25 17:58:06
 */
@Controller
@RequestMapping("/")
public class MidCheckController {
    @Autowired
    private MidCheckService midCheckService;


    /**
    * 跳转到页面
    */
    @GetMapping("/index/midCheck")
    public String midCheck() {
        return "midcheck/list";
        }

    @ApiOperation(value = "新增")
    @PostMapping("midCheck/add")
    @RequiresPermissions("midCheck:add")
    @ResponseBody
    public DataResult add(@RequestBody List<KeyValueVo> list){
//        midCheckService.save(midCheck);
        MidCheckEntity midCheckEntity =new MidCheckEntity();
        midCheckEntity.setStuName(KeyValueUtils.findKV(list, "stuName").getValue());
        midCheckEntity.setStuNumber(KeyValueUtils.findKV(list, "stuNumber").getValue());
        String jsonContent = JSON.toJSONString(list);
        midCheckEntity.setContentJson(jsonContent);
        midCheckEntity.setCreateTime(new Date());
        midCheckEntity.setUpdateTime(new Date());

        midCheckService.save(midCheckEntity);
        return DataResult.success();
    }

    @ApiOperation(value = "删除")
    @DeleteMapping("midCheck/delete")
    @RequiresPermissions("midCheck:delete")
    @ResponseBody
    public DataResult delete(@RequestBody @ApiParam(value = "id集合") List<String> ids){
        midCheckService.removeByIds(ids);
        return DataResult.success();
    }

    @ApiOperation(value = "更新")
    @PutMapping("midCheck/update")
    @RequiresPermissions("midCheck:update")
    @ResponseBody
    public DataResult update(@RequestBody MidCheckEntity midCheck){
        midCheckService.updateById(midCheck);
        return DataResult.success();
    }

    @ApiOperation(value = "查询分页数据")
    @PostMapping("midCheck/listByPage")
    @RequiresPermissions("midCheck:list")
    @ResponseBody
    public DataResult findListByPage(@RequestBody MidCheckEntity midCheck){
        Page page = new Page(midCheck.getPage(), midCheck.getLimit());
        LambdaQueryWrapper<MidCheckEntity> queryWrapper = Wrappers.lambdaQuery();
        //查询条件示例
        //queryWrapper.eq(MidCheckEntity::getId, midCheck.getId());
        IPage<MidCheckEntity> iPage = midCheckService.page(page, queryWrapper);
        return DataResult.success(iPage);
    }

}
